app.controller('pv9', function ($scope, $rootScope, $http, $interval, $compile, $state) {
    //附件
    $scope.pv1_0 = function () {
        $state.go(pv1_0.stateName);
    }

    //提交数据
    $scope.submit = function () {
        if(!$scope.caseId){
            return;
        }
        var remark = $(".remark").val();
        if(remark.length > 200){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注字数不能超过200个字"));
            return;
        }
        if(remark.length == 0 && $scope.imageUrls.length == 0){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "备注或拍照不能为空"));
            return;
        }
        if($scope.imageUrls.length!=0 &&($scope.imageUrls.length < 3 || $scope.imageUrls.length > 9)){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "拍照最少3张，最多9张"));
            return;
        }

        var postData = {
            "caseId": $scope.caseId,
            "remark":remark,
            "ocrUsageInfoPage": $scope.ocrUsageInfoPage,
            "imageUrls": $scope.imageUrls,
            "feSignUrl": $scope.feSignUrl,
            "customerSignUrl": $scope.customerSignUrl,
            "customerLongitude":$scope.blhLong,
            "customerLatitude":$scope.blhLat
        };

        var isfond = localStorage.getItem("pv-isfond");
        postData.isFond = isfond;

        console.log(postData);
        // alert(JSON.stringify(postData));
        $scope.result = JSON.stringify(postData);

        postHttp($http, 'pvCase/commitOcrDetail', postData, function (res) {
            if (res.code === 0 && res.msg == "success") {
                $scope.removeStorge();
                $scope.$applyAsync();
                $scope.pv1_0();
            } else {
                $rootScope.tipPopups.show(($rootScope.tipMessage = res.msg));
            }
        });

    };

    //清除storage数据
    $scope.removeStorge = function () {
        var token = getToken();
        //localStorage.clear();
        hpclearCache();
        sessionStorage.setItem("saveCheck", token);
    }

    //拍照
    $scope.takePhoto = function () {
        if($scope.imageUrls.length >= 9){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "最多上传9张图片"));
            return;
        }
        /*$scope.loadindex = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });*/
        var scanType = "pv1UploadImageResult";
        if(checkPhone('android')){
            AndroidMethod.uploadImage(getToken(),scanType);
        }else if(checkPhone("iphone")){
            //context.uploadImage(getToken(),scanType);

            var token = getToken();
			window.webkit.messageHandlers.uploadImage.postMessage({"scanType":scanType});
//            var reqeustData = {};
//            reqeustData.username = "support@jbkj.net.cn";
//            reqeustData.username = "13691179771@163.com";
//            reqeustData.password = "123456";
//
//            //var url = "http://183.195.106.118:8150/fec-api/api/login";
//            var url = "https://cnmpse2e-itg.ext.hp.com/fec-api/api/login";
//            postHttp2($http, url, reqeustData, function (res) {
//                if (res.msg === 'success') {
//                    token = res.token;
//                    
//                }
//            });

        }else{
			  hm.uploadImage(getToken(),scanType);
		}
    }
    //从相册选择
    $scope.takePhotoFromSystem = function () {
        if($scope.imageUrls.length >= 9){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "最多上传9张图片"));
            return;
        }

        var scanType = "pv1UploadImageResult";
        if(checkPhone('android')){
            AndroidMethod.uploadImageFromPhoto(getToken(),scanType);
        }else if(checkPhone("iphone")){
            $rootScope.tipPopups.show(($rootScope.tipMessage = "暂不支持"));
            return;
           /* var token = getToken();
            context.uploadImage(token,scanType);*/
        }else{
			hm.uploadImageFromPhoto(getToken(),scanType,$scope.imageUrls.length);
		}
        /*$scope.loadindex = layer.load(1, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
        });*/
    }

    //存入数组图片路径
    $scope.photoResult = function (url) {
       /* if(typeof(url)=='string'){
            $scope.imageUrls.push(JSON.parse(url).imageurl);
        }else{
            $scope.imageUrls.push(url.imageurl);
        }
        $scope.$applyAsync();*/
        //关闭加载
        // layer.close($scope.loadindex);
        //if (navigator.onLine){ //正常工作
            if(typeof(url)=='string'){
               var jsonData = JSON.parse(url).imageurl;
               for (var i = 0; i < jsonData.length; i++) {
                   var counter = jsonData[i].url;
               	 $scope.imageUrls.push(counter);
               }
            }else{
                $scope.imageUrls.push(url.imageurl);
            }
            $scope.$applyAsync();
        /*}else { //执行离线状态时的任务
            $rootScope.tipPopups.show(($rootScope.tipMessage = "当前网络不佳，稍后再试"));
            return;
        }*/

    }

    //删除图片
    $scope.removeImg = function (index) {
        $scope.imageUrls.splice(index,1);
        $scope.$applyAsync();
    }

    //初始化
    function init() {
        //案例id
        $scope.caseId = pv9.params.caseId;
        if (!pv9.params.caseId) {
            $scope.caseId = localStorage.getItem("pv-caseId");
        }
        $scope.blhLong = pv9.params.customeBlhLong;
        $scope.blhLat = pv9.params.customeBlhLat;
        $scope.ocrUsageInfoPage = [];

        if(localStorage.getItem("pv-ocrUsageInfoPage")){
            $scope.ocrUsageInfoPage = JSON.parse(localStorage.getItem("pv-ocrUsageInfoPage"));
        }

        $scope.imageUrls = [];

        $scope.feSignUrl = "";  //工程师签名
        if(localStorage.getItem("pv-feSignUrl")){
            $scope.feSignUrl = JSON.parse(localStorage.getItem("pv-feSignUrl"));
        }
        $scope.customerSignUrl = ""; //客户签名
        if(localStorage.getItem("pv-customerSignUrl")){
            $scope.customerSignUrl = JSON.parse(localStorage.getItem("pv-customerSignUrl"));
        }
    }
    init();

    reloadJs();

});
//返回扫描结果
function pv1UploadImageResult(msg) {
    var $scope = getScope("pv9");
    $scope.photoResult(msg);
}